﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace MyChart
{
    public class GdChart:ChartBase
    {
        int m_namex;
        int m_namey;
        int m_preDzRows;
        int m_peix1;
        int m_peix2;
        int m_peiy1;
        int m_peiy2;

        public List<PeiXianDz> peiXianDzList = new List<PeiXianDz>();
        DataTable m_dt;
        public GdChart()
        {
            ReadCfg();
        }

        public GdChart(DataTable dt)
        {
            m_dt = dt;
            ReadCfg();
            InitChart(dt);
        }
        
        private void ReadCfg()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("./\\fxp.xml");
            XmlNode root = doc.DocumentElement;
            XmlNode node2;
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称dgx");
            int.TryParse(node2.Value.ToString(), out m_namex);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称dgy");
            int.TryParse(node2.Value.ToString(), out m_namey);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@每个端子所占行数");
            int.TryParse(node2.Value.ToString(), out m_preDzRows);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称peix1");
            int.TryParse(node2.Value.ToString(), out m_peix1);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称peix2");
            int.TryParse(node2.Value.ToString(), out m_peix2);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称peiy1");
            int.TryParse(node2.Value.ToString(), out m_peiy1);
            node2 = root.SelectSingleNode("descendant::轨道/端子/@距离名称peiy2");
            int.TryParse(node2.Value.ToString(), out m_peiy2);
        }

        public override void InitChart(DataTable dt)
        {
            int id = 0;
            int rowIndex = 0;
            int colIndex = 0;
            bool isFindName = false;
            for (; rowIndex < dt.Rows.Count; rowIndex++)
            {
                DataRow row = dt.Rows[rowIndex];
                if (isFindName == false)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        string strVaule = row[col].ToString().Trim();
                        strVaule = strVaule.Replace(" ", "");
                        if (strVaule.Contains("名称"))
                        {
                            isFindName = true;
                            colIndex = dt.Columns.IndexOf(col);
                            break;
                        }
                    }
                }
                else
                {
                    if(++rowIndex>=dt.Rows.Count) break;
                    GdPeiXianDz gdPeiXianDz = new GdPeiXianDz(m_namex+colIndex, rowIndex, m_peix1+colIndex, rowIndex,
                                                              m_peix2+colIndex, rowIndex);
                    peiXianDzList.Add(gdPeiXianDz);
                    gdPeiXianDz.InitChart(dt);
                }
            }
        }
    }
}
